<?php
class TicketController extends BaseController
{
    // {$url}/ticket/issue
    // 火车票出票
    public function issueAction()
    {
    	
        $startTime = explode(' ', microtime());

        $db           = $this->mysql();
        $ticket_queue = $db->select('ticket_queue', '*', [
            'remarks' => '',
            'state'   => '待出票',
            'status'  => '火车票',
        ]);
        echo '<pre>';
        print_r($ticket_queue);
        if (!is_null($ticket_queue)) {
            foreach ($ticket_queue as $key => $value) {
                // 查询订单是否存在
                if (!$db->has('ticket_order', [
                    'order_number' => $value['order_number'],
                ])) {
                    dump_log('订单提交： ' . $value['order_number'] . ' 状态： 提交失败定不存在', 'ticket_order_error');
                    $db->delete('ticket_queue', [
                        'id' => $value['id'],
                    ]);
                    continue;
                }
                $param            = [];
                $obj              = new TicketClient('/train/searchOrderDetail/json');
                $param['orderNo'] = $value['orderNo'];
                $result           = $obj->getApiData($param);

                echo '<Pre>';
                print_r($result);
                // die();
                if ($result['rsCode'] == '000000') {
                    if ($result['rsData']['orderStatus'] == 3) {
                        $res = $db->update('ticket_order', [
                            'updatetime' => time(),
                            'pay_status' => '已出票',
                        ], [
                            'order_number' => $value['order_number'],
                        ]);
                    }
                    if ($result['rsData']['orderStatus'] == 4) {
                        $res = $db->update('ticket_order', [
                            'updatetime' => time(),
                            'pay_status' => '出票失败',
                        ], [
                            'order_number' => $value['order_number'],
                        ]);
                    }
                    if ($res->rowCount() > 0) {
                        $db->delete('ticket_queue',[
                            'order_number' => $value['order_number'],
                        ]);
                    }
                    //     $orderData = $result['rsData'];
                } else {
                    dump_log('火车票订单提交:' . $value['orderNo'] . '未获取到', 'huochepiao_error');
                    continue;
                }
                // // print_r();

                // $obj = new TicketClient('/train/payOrder/json');

                // $param                   = [];
                // $param['orderNo']        = $value['orderNo'];
                // $param['payType']        = 1;
                // $param['payerLoginName'] = '13070075714';
                // $param['totalPay']       = $orderData['orders'][0]['orderPrice'];

                // $result = $obj->getApiData($param);
                // echo '<pre>';
                // var_dump($result);
                // print_r($result);
                // if ($result['rsCode'] != '000000') {
                //     $db->update('ticket_queue', [
                //         'updatetime' => time(),
                //         'state'      => '数据异常',
                //         'remarks'    => $result['rsMessage'],
                //     ], [
                //         'order_number' => $value['order_number'],
                //         'orderNo'      => $value['orderNo'],
                //     ]);
                // } else {
                //     $res = $db->update('ticket_order', [
                //         'updatetime'  => time(),
                //         'commit_info' => json_encode($result['rqData']),
                //     ], [
                //         'order_number' => $value['order_number'],
                //     ]);
                //     if ($res->rowCount() > 0) {
                //         $db->update('ticket_queue', [
                //             'state'      => '待出票',
                //             'updatetime' => time(),
                //         ], [
                //             'order_number' => $value['order_number'],
                //             'status'       => '火车票',
                //             'orderNo'      => $value['orderNo'],
                //         ]);
                //     }
                // }

            }
        }

    }
}
